﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace h1
{
    public partial class Form_createList : Form
    {
        string id;
        public Form_createList()
        {
            InitializeComponent();
        }
        public Form_createList(string id)
        {
            InitializeComponent();
            this.id = id;
        }
        private void Form_createList_Load(object sender, EventArgs e)
        {
            

        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                DialogResult erorr = System.Windows.Forms.MessageBox.Show("请输入清单名称！", "提示", MessageBoxButtons.OK);
            }
            else if (comboBox1.Text == "")
            {
                DialogResult erorr = System.Windows.Forms.MessageBox.Show("请选择清单图标！", "提示", MessageBoxButtons.OK);
            }
            else
            {
                DataSet ds = MySqlHelper.getDataSet(MySqlHelper.Conn, $"SELECT * FROM lists WHERE userID = '{id}'");
                string listid = "00000";
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["listName"].ToString() == textBox1.Text)
                    {
                        DialogResult erorr = System.Windows.Forms.MessageBox.Show("该清单名称已存在！", "提示", MessageBoxButtons.OK);
                        return;
                    }
                    if (dr["listID"].ToString().Substring(dr["listID"].ToString().Length-5).CompareTo(listid) > 0)
                    {
                        listid = dr["listID"].ToString().Substring(dr["listID"].ToString().Length - 5);
                    }
                }
                DataRow newRow = ds.Tables[0].NewRow();
                listid = (Convert.ToUInt32(listid) + 1).ToString().PadLeft(5, '0');
                newRow["listID"] = id+listid;
                newRow["listName"] = textBox1.Text;
                newRow["listIcon"] = comboBox1.Text;
                newRow["userID"] = id;
                ds.Tables[0].Rows.Add(newRow);
                MySqlHelper.updateDataSet(MySqlHelper.Conn, $"SELECT * FROM lists WHERE userID = '{id}'", ds);
                DialogResult succeed = System.Windows.Forms.MessageBox.Show($"新建清单成功！清单名称为{textBox1.Text}", "提示", MessageBoxButtons.OK);
                this.Close();
            }
        }
    }
}
